package com.jonniespratley.flexdatabasemanager.factories
{
	import com.adobe.serialization.json.JSON;
	import com.jonniespratley.flexdatabasemanager.utils.PagedCollection;
	
	import mx.collections.ArrayCollection;
	
	public class FlexDatabaseManagerFactory
	{
		public function FlexDatabaseManagerFactory(){}
		
		/**
		 * Takes JSON result from service call and creates a new array collection. 
		 * @param jsonObj
		 * @return 
		 * 
		 */		
		static public function parseJSONResult( jsonObj:Object ):ArrayCollection
		{
			if ( jsonObj is String )
			{
				//get the raw JSON data and cast to String
				var rawData:String = String( jsonObj );
				
				//decode the data to ActionScript 
				var tempArray:Array = ( JSON.decode( rawData ) as Array );
					
				//var tempAC:ArrayCollection = new ArrayCollection( tempArray );
						
			}
			return  new ArrayCollection( tempArray );
		}
		
		/**
		 * Takes JSON result from service call, and loops through the object
		 * adding it to the array of value objects.
		 *  
		 * @param result
		 * @param valueObject
		 * @return 
		 * 
		 */		
		static public function parseJSONResultToValueObject( result:Object, valueObject:Object ):ArrayCollection
		{
			var tempArray:Array;
			if ( result is String )
			{
				//get the raw JSON data and cast to String
				var rawData:String = String( result );
		
				//decode the data to ActionScript 
				 var jsonArray:Array = ( JSON.decode( rawData ) as Array );
						
				for ( var s:String in jsonArray )
				{			
					tempArray.push( valueObject( jsonArray[s] ) );
				}
			}
			return new ArrayCollection( tempArray );
		}
		
		/**
		 *  Takes JSON result from service call and creates a new paged collection.
		 * @param jsonObj
		 * @return 
		 * 
		 */		
		static public function parseJSONToPagedCollection( jsonObj:Object ):PagedCollection
		{
			if ( jsonObj is String )
			{
				//get the raw JSON data and cast to String
				var rawData:String = String( jsonObj );

				//decode the data to ActionScript 
				var tempArray:Array = ( JSON.decode( rawData ) as Array );
			
				//var tempPC:PagedCollection = new PagedCollection( tempArray );
			}
			return new PagedCollection( tempArray );
		}
	}
}